//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet
{
    internal static class ItemsHelper   
    {
        public static IEnumerable<T> ReadItems<T>(ReadContext c, string webpublishitems) where T : IOfficeXmlSerializable, new()
        {
            if (c.ReaderLocalName == webpublishitems)
            {
                using (var c2 = c.Read(webpublishitems))
                {
                    while (c2.ReaderLocalName != null)
                        yield return c2.ReadElement<T>();
                }
            }
        }

        public static void WriteItems<T>(WriteContext context, List<T> items, string names) where T : IOfficeXmlSerializable
        {
            if (items == null || items.Count == 0)
                return;
            {
                using (var c2 = context.Write(names))
                {
                    c2.SetInteger("count", items.Count);
                    c2.WriteElements(items);
                }
            }
        }
    }
}